View Javadoc
1   package org.apache.maven.surefire.common.junit48;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.group.match.GroupMatcher;
23  import org.apache.maven.surefire.group.parse.GroupMatcherParser;
24  import org.apache.maven.surefire.group.parse.ParseException;
25  import org.apache.maven.surefire.testset.TestListResolver;
26  import org.junit.runner.manipulation.Filter;
27  
28  import java.util.Map;
29  import java.util.Set;
30  
31  import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
32  import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
33  import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank;
34  
35  /**
36   * @author Todd Lipcon
37   */
38  public class FilterFactory
39  {
40      private final ClassLoader testClassLoader;
41  
42      public FilterFactory( ClassLoader testClassLoader )
43      {
44          this.testClassLoader = testClassLoader;
45      }
46  
47      /**
48       * @return <tt>true</tt> if non-blank
49       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
50       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP} exists.
51       */
52      public boolean canCreateGroupFilter( Map<String, String> providerProperties )
53      {
54          String groups = providerProperties.get( TESTNG_GROUPS_PROP );
55          String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
56          return isNotBlank( groups ) || isNotBlank( excludedGroups );
57      }
58  
59      /**
60       * Creates filter using he key
61       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_GROUPS_PROP} and/or
62       * {@link org.apache.maven.surefire.booter.ProviderParameterNames#TESTNG_EXCLUDEDGROUPS_PROP}.
63       */
64      public Filter createGroupFilter( Map<String, String> providerProperties )
65      {
66          String groups = providerProperties.get( TESTNG_GROUPS_PROP );
67  
68          GroupMatcher included = null;
69          if ( isNotBlank( groups ) )
70          {
71              try
72              {
73                  included = new GroupMatcherParser( groups ).parse();
74              }
75              catch ( ParseException e )
76              {
77                  throw new IllegalArgumentException(
78                      "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e );
79              }
80          }
81  
82          String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
83  
84          GroupMatcher excluded = null;
85          if ( isNotBlank( excludedGroups ) )
86          {
87              try
88              {
89                  excluded = new GroupMatcherParser( excludedGroups ).parse();
90              }
91              catch ( ParseException e )
92              {
93                  throw new IllegalArgumentException(
94                      "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e );
95              }
96          }
97  
98          if ( included != null && testClassLoader != null )
99          {
100             included.loadGroupClasses( testClassLoader );
101         }
102 
103         if ( excluded != null && testClassLoader != null )
104         {
105             excluded.loadGroupClasses( testClassLoader );
106         }
107 
108         return new GroupMatcherCategoryFilter( included, excluded );
109     }
110 
111     public Filter createMethodFilter( String requestedTestMethod )
112     {
113         return new MethodFilter( requestedTestMethod );
114     }
115 
116     public Filter createMethodFilter( TestListResolver resolver )
117     {
118         return new MethodFilter( resolver );
119     }
120 
121     public Filter createFailingMethodFilter( Map<Class<?>, Set<String>> failingClassMethodMap )
122     {
123         return new FailingMethodFilter( failingClassMethodMap );
124     }
125 
126     public Filter and( Filter filter1, Filter filter2 )
127     {
128         return new AndFilter( filter1, filter2 );
129     }
130 }